package typedefault

import (
	"codejy/globalfunc"
	"codejy/store"
	"codejy/structdef"
	"codejy/utils"
	"strconv"
	"strings"
)

type TypeStr struct {
	Addr        string
	ThreadStore *structdef.ThreadStore
}

func (a *TypeStr) CreateTypeDefault(addr string, threadStore *structdef.ThreadStore, parameter ...string) {
	str := &TypeStr{
		Addr:        addr,
		ThreadStore: threadStore,
	}
	store.AddTypeDefaultInstanceStore(threadStore, a.GetTypeName(), addr, str)
}
func (a *TypeStr) GetData() interface{} {
	return nil
}
func (a *TypeStr) ToStr() string {
	return ""
}

func (a *TypeStr) ParameterProcessing(parameter []string) []string {
	for i := range parameter {
		StrToHandle(&parameter[i])
	}
	return parameter
}

// 获取字符串长度
func (a *TypeStr) LenStr(str string) string {
	return strconv.Itoa(len([]rune(str)))
}

//截取字符串
/**
 * @param str 字符串
 * @param start 开始位置
 * @param end 结束位置
 */
func (a *TypeStr) SubStr(str string, start string, end string) string {
	atoi, _ := strconv.Atoi(start)
	atoi2, _ := strconv.Atoi(end)
	runes := []rune(str)
	return StringToStr(string(runes[atoi:atoi2]))
}

// 获取b字符串在a字符串的下标 ,返回-1没有找到
func (a *TypeStr) Index(aStr string, bStr string) string {
	return strconv.Itoa(utils.GetStrIndex(aStr, bStr))
}

// 获取b字符串在a字符串的最后下标 ,返回-1没有找到
func (a *TypeStr) LastIndex(aStr string, bStr string) string {
	return strconv.Itoa(utils.GetStrLastIndex(aStr, bStr))
}

// 字符串替换,替换所有符合条件的
/**
 * @param aStr 字符串
 * @param oldStr 被替换的字符串
 * @param newStr 替换的字符串
 */
func (a *TypeStr) Replace(aStr string, oldStr string, newStr string) string {
	return StringToStr(strings.Replace(aStr, oldStr, newStr, -1))
}

//判断字符串开头是否是b字符串
/**
 * @param aStr 字符串
 * @param bStr 开头字符串
 */
func (a *TypeStr) HasPrefix(aStr string, bStr string) string {
	return strconv.FormatBool(strings.HasPrefix(aStr, bStr))
}

//判断字符串结尾是否是b字符串
/**
 * @param aStr 字符串
 * @param bStr 结尾字符串
 */
func (a *TypeStr) HasSuffix(aStr string, bStr string) string {
	return strconv.FormatBool(strings.HasSuffix(aStr, bStr))
}

//判断是否包含b字符串
/**
 * @param aStr 字符串
 * @param bStr 包含字符串
 */
func (a *TypeStr) Contains(aStr string, bStr string) string {
	return strconv.FormatBool(strings.Contains(aStr, bStr))
}

//字符串转换为小写
/**
 * @param aStr 字符串
 */
func (a *TypeStr) ToLower(aStr string) string {
	return StringToStr(strings.ToLower(aStr))
}

//字符串转换为大写
/**
 * @param aStr 字符串
 */
func (a *TypeStr) ToUpper(aStr string) string {
	return StringToStr(strings.ToUpper(aStr))
}

//复制n次字符串
/**
 * @param aStr 字符串
 * @param nStr 复制次数
 */
func (a *TypeStr) Repeat(aStr string, nStr string) string {
	atoi, _ := strconv.Atoi(nStr)
	return StringToStr(strings.Repeat(aStr, atoi))
}

// 去除字符串两边的空格
func (a *TypeStr) Trim(aStr string) string {
	aStr = strings.TrimSpace(aStr)
	return StringToStr(aStr)
}

// 去除所有空格
func (a *TypeStr) TrimAll(aStr string) string {
	aStr = strings.Replace(aStr, " ", "", -1)
	return StringToStr(aStr)
}

// 获取字符串子串出现的次数
func (a *TypeStr) Count(aStr, bStr string) string {
	return strconv.Itoa(strings.Count(aStr, bStr))
}

// 字符串转bytes
func (a *TypeStr) StringToBytes(str string) string {
	bytes := []byte(str)
	return globalfunc.ExecuteFunc("TransitionBytes", bytes, a.ThreadStore).(string)
}

// 多行字符串转List
func (a *TypeStr) StringsToList(str string) string {
	strs := strings.Split(str, "\n")
	var addrList = globalfunc.ExecuteFunc("SliceToList", strs, a.ThreadStore).(string)
	return addrList
}

func (f *TypeStr) GetDef() string {
	return `
		@CodeManual(def="Str",des="字符串处理工具,Str.xxx")
		type share Str{
			def init{
				core&CreateTypeDefault()
			}
			@CodeManual(def="Str.size($1)",des="获取字符串大小")
			def size{
				to core&LenStr($1) 			 
 			}	
			@CodeManual(def="Str.sub($1,$2,$3)",des="截取$1字符串,$2开始位置,$3结束位置")
			def sub{
				to core&SubStr($1,$2,$3) 			 
 			}
			@CodeManual(def="Str.index($1,$2,$3)",des="获取$1中第一个$2的下标")
			def index{
				to core&Index($1,$2) 			 
 			}
			
			@CodeManual(def="Str.lastIndex($1,$2)",des="获取$1中最后$2的下标")
			def lastIndex{
				to core&LastIndex($1,$2) 			 
 			}

			@CodeManual(def="Str.replace($1,$2,$3)",des="替换,将$1中包含$2的字符替换为$3")
			def replace{
				to core&Replace($1,$2,$3) 			 
 			}
			
			@CodeManual(def="Str.prefix($1,$2)",des="判断$1的前缀是否是$2")
			def prefix{
				to core&HasPrefix($1,$2) 			 
 			}
			
			@CodeManual(def="Str.suffix($1,$2)",des="判断$1的后缀是否是$2")
			def suffix{
				to core&HasSuffix($1,$2) 			 
 			}
			
			@CodeManual(def="Str.contains($1,$2)",des="判断$1是否包含$2")
			def contains{
				to core&Contains($1,$2) 			 
 			}
			@CodeManual(def="Str.toLower($1,$2)",des="将$1转小写")
			def toLower{
				to core&ToLower($1) 			 
 			}
		
			@CodeManual(def="Str.toUpper($1,$2)",des="将$1转大写")
			def toUpper{
				to core&ToUpper($1) 			 
 			}
			
			@CodeManual(def="Str.repeat($1,$2)",des="复制$1字符串$2次")
			def repeat{
				to core&Repeat($1,$2) 			 
 			}
			
			@CodeManual(def="Str.trim($1)",des="将$1字符串去除两边空格")
			def trim{
				to core&Trim($1)
			}
			@CodeManual(def="Str.trimAll($1)",des="将$1字符串去除所有空格")
			def trimAll{
				to core&TrimAll($1)
			}
			@CodeManual(def="Str.count($1,$2)",des="获取$1字符串中$2出现的次数")
			def count{
				to core&Count($1,$2)	
			}

			@CodeManual(def="Str.toBytes($1)",des="将$1字符串转bytes")
			def toBytes{
				to core&StringToBytes($1)
			}

			@CodeManual(def="Str.toList($1)",des="将$1多行字符串转List,字符是以\n分割的")
			def toList{
				to core&StringsToList($1)
			}
		}
	`
}

func init() {
	RegisterTypeDefault(&TypeStr{})
}

func (f *TypeStr) GetTypeName() string {
	return "Str"
}

// 转换为字符串
func StrToString(str string) string {
	//去掉"
	if strings.HasPrefix(str, "\"") && strings.HasSuffix(str, "\"") {
		str = str[1 : len(str)-1]
	}
	return str
}

// 返回字符串
func StringToStr(str string) string {
	return "\"" + str + "\""
}

// \n \t \r处理
func SpecificSymbol(string2 string) string {
	if string2 == "" {
		return string2
	}
	//判断开头是x:\\
	if strings.HasPrefix(string2[1:], ":\\") {
		return string2
	}
	//判断开头是x://
	if strings.HasPrefix(string2[1:], "://") {
		return string2
	}
	//判断字符串开头  是/ 是./和../ ,那么就是路径,不处理
	if strings.HasPrefix(string2, "/") || strings.HasPrefix(string2, "./") || strings.HasPrefix(string2, "../") {
		return string2
	}

	//判断是否存在\n
	if strings.Contains(string2, "\\n") {
		//将字符串中换行符替换为\n
		string2 = strings.Replace(string2, "\\n", "\n", -1)
	}
	//判断是否存在\t
	if strings.Contains(string2, "\\t") {
		//将字符串中的\t替换为\t
		string2 = strings.Replace(string2, "\\t", "\t", -1)
	}
	//判断是否存在\r
	if strings.Contains(string2, "\\r") {
		//将字符串中的\r替换为\r
		string2 = strings.Replace(string2, "\\r", "\r", -1)
	}
	return string2
}

// 处理字符串,去除引号,和特殊符号(内置函数,不对外开放)
func StrToHandle(str *string) {
	//判断是否是字符串
	if strings.HasPrefix(*str, "\"") && strings.HasSuffix(*str, "\"") {
		//去除引号
		*str = StrToString(*str)
		//处理特殊符号,还原
		*str = SpecificSymbol(*str)
	}
}
